#include"scene.h"
#include"fonction.h"
#include<allegro.h>
#include<stdlib.h>
#include<stdio.h>
#include"intel.h"

int indi=0;
FILE *f;
/*initialisation de toutes les images*/
void initialiser(heros *hero,guru *gur1)
{
	for(i=0;i<3;i++)
	{
		if(hero->attack_sabrel[i]==NULL || hero->attack_sabrer[i]==NULL || hero->attack_martr[i]==NULL || hero->attack_martr[i]==NULL)
		{allegro_message("could not load images");
	 	allegro_exit();}
	}

	for(i=0;i<4;i++)
	{	if(gur1->marteau[i]==NULL){allegro_message("could not load images");
	 				   allegro_exit();}
	}

	for(i=0;i<6;i++)
	{
		if(hero->damm[i]==NULL){allegro_message("could not load images");
	 				allegro_exit();}
	}	

	for(i=0;i<5;i++)
	{
		if(gur1->img_right[i]==NULL || gur1->img_left[i]==NULL){allegro_message("could not load images");
	 							allegro_exit();}
	}
	if(hero->img_right==NULL || hero->img_left==NULL){allegro_message("could not load images");
	 							allegro_exit();}
	
}

/*la boucle de la premiere scene*/
int scene1(int ind,heros *hero,guru *gur1,BITMAP* background){
	(*hero).x=(SCREEN_W/2)-550 ;
	(*hero).y=(SCREEN_H/2)-120;
	(*gur1).x=(SCREEN_W/2)+600 ;
	(*gur1).y=(SCREEN_H/2)-120;
	blit(background,buffer,0,0,0,0,background->w,background->h);
        
	

	image4=hero->damm[0];
	image=hero->img_right;
	image2=gur1->img_left[0];
	draw_sprite(buffer,hero->img_right,(*hero).x,(*hero).y);
        draw_sprite(buffer,image2,(*gur1).x,(*gur1).y);
	draw_sprite(buffer,image4,image4->w,image4->h);
	blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
	i=0;
	for(j=0;j<40;j++){
				if(i>4)i=0;
				image2=gur1->img_left[i];
				i++;(*gur1).x=(*gur1).x-7;
				blit(background,buffer,0,0,0,0,background->w,background->h);
				draw_sprite(buffer,hero->img_right,(*hero).x,(*hero).y);
				draw_sprite(buffer,image2,(*gur1).x,(*gur1).y);
				draw_sprite(buffer,image4,image4->w,image4->h);
				blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
				attendre(0.01);}
		
	nbre=1;
	
while((*hero).blood!=0 && nbre!=0){
		
		
		if(image4==hero->damm[5]){ind=0;return ind;}
		if((*gur1).x-((*hero).x)<0)image2=gur1->img_right[0];
		if((*gur1).x-((*hero).x)>0)image2=gur1->img_left[0];
		
		if (key[KEY_RIGHT] && (*hero).x < SCREEN_W -100)
		translaterdroit(hero,gur1);

		if (key[KEY_LEFT] && (*hero).x > 50)
		transletergauche(hero,gur1);

    		if(key[KEY_UP] && (*hero).y < SCREEN_H)
		sauter(hero,gur1);
		
		
		if(key[KEY_A] && (*hero).y < SCREEN_H && (*hero).x < SCREEN_W -100 && (*hero).x > 50)
		sursauter(hero,gur1);

         if (((*gur1).x-(*hero).x)==800)lancermarteau(hero,gur1);

	if(key[KEY_Z]){
			attaquersabre(hero,gur1,&nbre);
			}
		

if(key[KEY_ESC]){while(indi == 0){
				show_mouse(screen);
				blit(background,buffer,0,0,0,0,background->w,background->h);
				draw_sprite(buffer,hero->img_right,(*hero).x,(*hero).y);
				draw_sprite(buffer,image2,(*gur1).x,(*gur1).y);
				draw_sprite(buffer,image4,image4->w,image4->h);
				draw_sprite(buffer,pau[0],SCREEN_W/4,SCREEN_H/7);
				blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
		while(mouse_x>570 && mouse_x<850 && mouse_y<330 && mouse_y>270 && indi!=1){
					    show_mouse(screen);
						blit(background,buffer,0,0,0,0,background->w,background->h);
						draw_sprite(buffer,hero->img_right,(*hero).x,(*hero).y);
						draw_sprite(buffer,image2,(*gur1).x,(*gur1).y);
						draw_sprite(buffer,image4,image4->w,image4->h);
						draw_sprite(buffer,pau[1],SCREEN_W/4,SCREEN_H/7);
						blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
						
							if(mouse_b & 1){
						                     indi=1;}}


		while(mouse_x>570 && mouse_x<850 && mouse_y<435 && mouse_y>370 && indi!=1){
					    show_mouse(screen);
						blit(background,buffer,0,0,0,0,background->w,background->h);
						draw_sprite(buffer,hero->img_right,(*hero).x,(*hero).y);
						draw_sprite(buffer,image2,(*gur1).x,(*gur1).y);
						draw_sprite(buffer,image4,image4->w,image4->h);
						draw_sprite(buffer,pau[2],SCREEN_W/4,SCREEN_H/7);
						blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);

							if(mouse_b & 1){k=fopen("save.txt","w");
											fprintf(k,"%d",ind);
											fclose(k);
						                     indi=1;}}


		while(mouse_x>570 && mouse_x<850 && mouse_y<530 && mouse_y>465 && ind!=0){
				        show_mouse(screen);
						blit(background,buffer,0,0,0,0,background->w,background->h);
						draw_sprite(buffer,hero->img_right,(*hero).x,(*hero).y);
						draw_sprite(buffer,image2,(*gur1).x,(*gur1).y);
						draw_sprite(buffer,image4,image4->w,image4->h);
						draw_sprite(buffer,pau[3],SCREEN_W/4,SCREEN_H/7);
						blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
						if(mouse_b & 1){
						                     ind=0;return ind;}}
				}
				blit(background,buffer,0,0,0,0,background->w,background->h);
				draw_sprite(buffer,hero->img_right,(*hero).x,(*hero).y);
				draw_sprite(buffer,image2,(*gur1).x,(*gur1).y);
				blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
           indi=0;
		

		}
}
if(nbre<=0){ind=2;return ind;}
attendre(1);
}	





int scene2(int ind,heros *hero,guru *gur1,BITMAP* background)
{	if(image4==hero->damm[5]){ind=0;return ind;}

	(*gur1).x=(SCREEN_W/2)+600 ;
	(*gur1).y=(SCREEN_H/2)+100;
	(*hero).x=(SCREEN_W/2)-550 ;
	(*hero).y=(SCREEN_H/2)+100;
	
	blit(background,buffer,0,0,0,0,background->w,background->h);
        
	

	image4=hero->damm[0];
	image=hero->img_right;
	image2=gur1->img_left[0];
	draw_sprite(buffer,hero->img_right,(*hero).x,(*hero).y);
        draw_sprite(buffer,image2,(*gur1).x,(*gur1).y);
	draw_sprite(buffer,image4,image4->w,image4->h);
	blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
	i=0;
	for(j=0;j<20;j++){
				if(i>4)i=0;
				image2=gur1->img_left[i];
				i++;(*gur1).x=(*gur1).x-7;
				blit(background,buffer,0,0,0,0,background->w,background->h);
				draw_sprite(buffer,hero->img_right,(*hero).x,(*hero).y);
				draw_sprite(buffer,image2,(*gur1).x,(*gur1).y);
				draw_sprite(buffer,image4,image4->w,image4->h);
				blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
				attendre(0.01);}
nbre=2	;
while((*hero).blood!=0 && nbre!=0)
{


		

		if((*gur1).x-((*hero).x)<0)image2=gur1->img_right[0];
		if((*gur1).x-((*hero).x)>0)image2=gur1->img_left[0];
		
		if (key[KEY_RIGHT] && (*hero).x < SCREEN_W -100)
		translaterdroit(hero,gur1);

		if (key[KEY_LEFT] && (*hero).x > 50)
		transletergauche(hero,gur1);

    		if(key[KEY_UP] && (*hero).y < SCREEN_H)
		sauter(hero,gur1);
		
		
		if(key[KEY_A] && (*hero).y < SCREEN_H && (*hero).x < SCREEN_W -100 && (*hero).x > 50)
		sursauter(hero,gur1);

         if (key[KEY_X]) lancermarteau(hero,gur1);

	if(key[KEY_Z]){
			attaquersabre(hero,gur1,&nbre);
			}
		

if(key[KEY_ESC]){while(indi == 0){
				show_mouse(screen);
				blit(background,buffer,0,0,0,0,background->w,background->h);
				draw_sprite(buffer,hero->img_right,(*hero).x,(*hero).y);
				draw_sprite(buffer,image2,(*gur1).x,(*gur1).y);
				draw_sprite(buffer,image4,image4->w,image4->h);
				draw_sprite(buffer,pau[0],SCREEN_W/4,SCREEN_H/7);
				blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
		while(mouse_x>570 && mouse_x<850 && mouse_y<330 && mouse_y>270 && indi!=1){
					    show_mouse(screen);
						blit(background,buffer,0,0,0,0,background->w,background->h);
						draw_sprite(buffer,hero->img_right,(*hero).x,(*hero).y);
						draw_sprite(buffer,image2,(*gur1).x,(*gur1).y);
						draw_sprite(buffer,image4,image4->w,image4->h);
						draw_sprite(buffer,pau[1],SCREEN_W/4,SCREEN_H/7);
						blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
						
							if(mouse_b & 1){
						                     indi=1;}}
while(mouse_x>570 && mouse_x<850 && mouse_y<435 && mouse_y>370 && indi!=1){
					    show_mouse(screen);
						blit(background,buffer,0,0,0,0,background->w,background->h);
						draw_sprite(buffer,hero->img_right,(*hero).x,(*hero).y);
						draw_sprite(buffer,image2,(*gur1).x,(*gur1).y);
						draw_sprite(buffer,image4,image4->w,image4->h);
						draw_sprite(buffer,pau[2],SCREEN_W/4,SCREEN_H/7);
						blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);

							if(mouse_b & 1){k=fopen("save.txt","w");
											fprintf(k,"%d",ind);
											fclose(k);
						                     indi=1;}}


		while(mouse_x>570 && mouse_x<850 && mouse_y<530 && mouse_y>465 && ind!=0){
				        show_mouse(screen);
						blit(background,buffer,0,0,0,0,background->w,background->h);
						draw_sprite(buffer,hero->img_right,(*hero).x,(*hero).y);
						draw_sprite(buffer,image2,(*gur1).x,(*gur1).y);
						draw_sprite(buffer,image4,image4->w,image4->h);
						draw_sprite(buffer,pau[3],SCREEN_W/4,SCREEN_H/7);
						blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
						if(mouse_b & 1){
						                     ind=0;return ind;}}
				}
				blit(background,buffer,0,0,0,0,background->w,background->h);
				draw_sprite(buffer,hero->img_right,(*hero).x,(*hero).y);
				draw_sprite(buffer,image2,(*gur1).x,(*gur1).y);
				blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
           indi=0;
		

		}}

}







